package es.uned.dia.james;

//Model description
public class Model {
	private double gain;
	private double[] zeros, poles;
	private int nzeros, npoles;
	
    public Model () {
		zeros = new double[10];
		poles = new double[10];
		nzeros = npoles = 0;
	}

// Adds a zero to the model
	public boolean addZero(double z) {
		if(nzeros<10) {
			zeros[nzeros++] = z;
			return true;
		} else 
			return false;
	}

// 	Remove a zero from the model. Returns true if the zero is removed, false if the zero is not found
	public boolean removeZero(double z) {
		boolean found = false;

		for(int i=0;i<nzeros;i++) {
			if(found && i<nzeros)   zeros[i]=zeros[i+1];
			else if(zeros[i] == z)  found = true;
		}
		nzeros--;

		return found;
	}

// Adds a pole to the model
	public boolean addPole(double p) {
		if(npoles<10) {
			poles[npoles++] = p;
			return true;
		} else
			return false;
	}
	
// Remove a pole from the model
	public boolean removePole(double p) {
		boolean found = false;

		for(int i=0;i<npoles;i++) {
			if(found && i<npoles)   poles[i]=poles[i+1];
			else if(poles[i] == p)  found = true;		
		}
		npoles--;

		return found;
	}

// Set the model gain
	public void setGain(double k) {
		gain = k;
	}
	
// Returns an array containing the model zeros
	public double[] Z() {
		return zeros;
	}

// Returns an array containing the model poles
	public double[] P() {
		return poles;
	}

// Returns the model gain
	public double K() {
		return gain;
	}

	public String getTeX() {
	    String result = "\\frac{";
	     
	    for(int i=0; i<nzeros; i++) result = result.concat("(s+"+zeros[i]+")");	        
        result = result.concat("}{");
        for(int i=0; i<npoles; i++) result = result.concat("(s+"+poles[i]+")");          
        result = result.concat("}");
        
        return result;
	}
}